package com.achievo.know.domain.infra.hibernate;

import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import com.achievo.know.domain.Function;
import com.achievo.know.domain.FunctionRepository;
import com.achievo.know.domain.FunctionType;
import com.dayatang.spring.repository.BaseEntityRepositoryHibernateSpring;

/**
 * 功能实体管理仓储实现
 *<br />
 * @author vakin.Jiang
 *2010-4-19
 */

@Repository
public class FunctionRepositoryImpl extends BaseEntityRepositoryHibernateSpring<Function, Long> implements FunctionRepository {

	public FunctionRepositoryImpl() {
		super(Function.class);
	}

	@Override
	public List<Function> findByParentId(Long parentId) {
		return findByNamedQuery("Function.findbyParentId", new Object[] { parentId });
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Function> getFunctionByType(FunctionType type) {
		return getSession().createCriteria(Function.class)
        .createAlias("functionType", "type")
        .add(Restrictions.eq("type.id", type.getId()))
        .list();
	}

	@Override
	public List<Function> getRootFuncs() {
		DetachedCriteria c=DetachedCriteria.forClass(Function.class);
		c.add(Restrictions.isNull("parent"));
		return getHibernateTemplate().findByCriteria(c);
	}
	
	
}
